package com.fs.sw.pojo;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import javax.persistence.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@NamedEntityGraphs({
	@NamedEntityGraph(name = "Roles.users",
			attributeNodes = {
			@NamedAttributeNode("users")
	}),
	@NamedEntityGraph(name = "roles-users-menu",
			attributeNodes = {
					@NamedAttributeNode(value = "users",subgraph = "users.countrys"),
					@NamedAttributeNode(value = "menus"),
			}
			,
			subgraphs = {
					@NamedSubgraph(name = "users.countrys",attributeNodes = {
							@NamedAttributeNode("countrys")
					})
//					,
//					@NamedSubgraph(name = "regions",attributeNodes = {
//							@NamedAttributeNode(value = "regions")
//					})
			}
		)
		,
		@NamedEntityGraph(name = "roles-users-menu-countrys",
				attributeNodes = {
						@NamedAttributeNode(value = "users",subgraph = "countrys"),
						@NamedAttributeNode(value = "menus"),
				}
				,
				subgraphs = {
						@NamedSubgraph(name = "countrys",attributeNodes = {
								@NamedAttributeNode(value = "countrys",subgraph = "regions")
						})
						,
						@NamedSubgraph(name = "regions",attributeNodes = {
								@NamedAttributeNode(value = "regions")
						})
				}
		)
})
@Entity
@Table(name="t_roles")
public class Roles {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="roleid")
	private Integer roleid;

	@Column(name="rolename")
	private String rolename;
	
	@OneToMany(mappedBy="roles", fetch = FetchType.LAZY)
//	@JSONField(serialize = false)
//	@JsonBackReference // data to json 和 JsonIgnore一样
//	@JsonManagedReference // data to json 会被序列化
//  @JsonIgnore // 直接忽略子实体
	private Set<Users> users ;

	@ManyToMany(cascade=CascadeType.PERSIST,fetch=FetchType.LAZY)
	//@JoinTable:映射中间表
	//joinColumns:当前表中的主键所关联的中间表中的外键字段
	@JoinTable(name="t_roles_menus",joinColumns=@JoinColumn(name="role_id"),inverseJoinColumns=@JoinColumn(name="menu_id"))
	private Set<Menus> menus = new HashSet<>();
	
	public Integer getRoleid() {
		return roleid;
	}

	public void setRoleid(Integer roleid) {
		this.roleid = roleid;
	}

	public String getRolename() {
		return rolename;
	}

	public void setRolename(String rolename) {
		this.rolename = rolename;
	}

	public Set<Users> getUsers() {
		return users;
	}

	public void setUsers(Set<Users> users) {
		this.users = users;
	}

	public Set<Menus> getMenus() {
		return menus;
	}

	public void setMenus(Set<Menus> menus) {
		this.menus = menus;
	}
	
}
